package com.jsonal.controller;

import com.jsonal.common.BusinessConstants;
import com.jsonal.common.StringEscapeEditor;
import com.jsonal.entity.BaseBean;
import com.jsonal.entity.api.UserBean;
import com.jsonal.entity.manager.UserManagerBean;
import com.jsonal.vo.Result;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * @Author: jingji.lin
 * @Description: ${description}
 * @Date: 2020/1/4 15:34
 * @Version: 1.0
 */
@SuppressWarnings("ALL")
public class BaseController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {

        binder.registerCustomEditor(Date.class, new CustomDateEditor(
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
            new SimpleDateFormat("yyyy-MM-dd"), true));
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false, true));
    }


    /**
     * 后端校验
     *
     * @return String 消息
     */
    protected String validation(BindingResult bindingResult) {

        // 无错误消息直接放回成功
        if (bindingResult.hasErrors() == false) {
            return BusinessConstants.NO_MISTAKE;
        }
        List<ObjectError> errorList = bindingResult.getAllErrors();

        String msg = BusinessConstants.EMPTY;

        for (ObjectError error : errorList) {

            msg += error.getDefaultMessage() + BusinessConstants.LINE_FEED;
        }

        return msg;

    }

    /**
     * Form to Bean
     *
     * @param form 来源
     * @param bean 基础
     */
    protected void setFormToBean(Object form, BaseBean bean) {

        BeanUtils.copyProperties(form, bean);
        bean.setCreateUser(this.getUserId());
        bean.setDelUser(this.getUserId());
        bean.setLastUpdateUser(this.getUserId());
        bean.setCurrentUser(this.getUserId());
    }

    protected void setFormToBean2(Object form, BaseBean bean) {
        BeanUtils.copyProperties(form, bean);
        UserManagerBean userBean = this.getCurrentUser();
        bean.setCurrentUser(userBean.getId());
    }

    protected void setFormToBean3(Object form, BaseBean bean) {
        BeanUtils.copyProperties(form, bean);
    }

    /**
     * 获取当前登录用户对象
     *
     * @return api
     */
    public UserManagerBean getCurrentUser() {
        UserManagerBean userBean = new UserManagerBean();

        userBean.setUsername(SecurityUtils.getSubject().getPrincipal().toString());

        return userBean;
    }

    /**
     * 获取当前登录用户id
     *
     * @return string
     */
    public String getUserId() {

        return this.getCurrentUser().getId();
//        return "kayisoft";
    }

    public Result getResult(String validMsg) {
        if (!validMsg.equals(BusinessConstants.NO_MISTAKE)) {

            Result result = new Result();
            result.setSuccess(false);
            result.setMsg(validMsg);

            return result;
        }
        return null;
    }
}
